<?php

/**
 * Obtenir la liste de tous les événements avec leur nombre de membre
 * @param viewer: id du membre qui demande la liste
 */
function event_get_list($viewer = 0)
{
	$viewer = intval($viewer);
	$sql = 'SELECT e.id, e.name, e.description, e.type, e.time_event, e.time_create,
	               (SELECT COUNT(d.idMember)
	                FROM EventDecision d
	                WHERE d.idEvent = e.id AND d.decision = "Y") AS count_join,
	               (SELECT d2.decision
	                FROM EventDecision d2
	                WHERE d2.idMember = '.$viewer.' AND d2.idEvent = e.id) AS viewer_decision
	        FROM Event e
	        ORDER BY e.time_event DESC';
	$data = sql_query($sql);
	return $data;
}

/**
 * Obtenir les détails d'un événement
 * @param id: id de l'event
 * @param viewer: id du membre qui demande l'event
 * @return array ou NULL
 */
function event_get_by_id($id, $viewer = 0)
{
	$id = intval($id);
	$viewer = intval($viewer);
	$sql = 'SELECT e.id, e.name, e.description, e.type, e.time_event, e.time_create,
			       (SELECT COUNT(d.idMember)
	                FROM EventDecision d
	                WHERE d.idEvent = e.id AND d.decision = "Y") AS count_join,
	               (SELECT d2.decision
	                FROM EventDecision d2
	                WHERE d2.idMember = '.$viewer.' AND d2.idEvent = e.id) AS viewer_decision
            FROM Event e
            WHERE e.id = '.$id.'
            ORDER BY e.time_event DESC';
	$data = sql_query($sql);
	return mysql_fetch_assoc($data);
}

/**
 * @param event: id de l'événement
 * @param member: id du membre
 * @return décision du membre s'il a pris une décision, sinon NULL
 */
function event_get_decision($event, $member)
{
	$sql = "SELECT decision
	        FROM EventDecision
	        WHERE idMember = $member AND idEvent = $event;";
	$data = mysql_fetch_assoc(sql_query($sql));
	return $data != NULL ? $data['decision'] : NULL;
}

/**
 * Obtenir la liste des membres qui ont répondu à un événement
 * @param id: id de l'event
 */
function event_members($id)
{
	$id = intval($id);
	$sql = "SELECT m.id, m.firstname, m.name, m.email, d.decision, d.date
	        FROM Member m
	        LEFT JOIN EventDecision d ON d.idMember = m.id
	        WHERE d.idEvent = $id
	        ORDER BY m.name, m.firstname;";
	$data = sql_query($sql);
	return $data;
}

/**
 * Ajouter un événement
 */
function event_add($name, $description, $type, $timestamp)
{
	$name = mysql_real_escape_string($name);
	$description = mysql_real_escape_string($description);
	$type = mysql_real_escape_string($type);
	$timestamp = intval($timestamp);
	$sql = 'INSERT INTO Event (name, description, type, time_event, time_create)
	        VALUES ("'.$name.'", "'.$description.'", "'.$type.'", '.$timestamp.', '.time().');';
	sql_query($sql);
}

/**
 * Mettre à jour un événement
 */
function event_update($id, $name, $description, $type, $timestamp)
{
	$id = intval($id);
	$name = mysql_real_escape_string($name);
	$description = mysql_real_escape_string($description);
	$type = mysql_real_escape_string($type);
	$timestamp = intval($timestamp);
	
	$sql = 'UPDATE Event
	        SET name = "'.$name.'", description = "'.$description.'", type = "'.$type.'", time_event = '.$timestamp.'
	        WHERE id = '.$id.';';
	sql_query($sql);
}

/**
 * Supprimer un événement et ses adhésions
 */
function event_delete($id)
{
	$id = intval($id);
	$sql = "DELETE FROM EventDecision
	        WHERE idEvent = $id;";
	sql_query($sql);
	$sql = "DELETE FROM Event
	        WHERE id = $id;";
	sql_query($sql);
}

/**
 * Nom d'un type d'événement
 */
function event_type_name($type)
{
	switch ($type) {
		case 'S':
			return 'Sortie';
		case 'C':
			return 'Compétition';
		case 'E':
			return 'Événement';
	}
	return '<unknown event>';
}

/**
 * @param event: id de l'évènement
 * @param member: id du membre
 * @param decision: Y ou N
 */
function event_set_decision($event, $member, $decision)
{
	$event = intval($event);
	$member = intval($member);
	$decision = mysql_real_escape_string($decision);
	$date = time();
	$sql = "INSERT INTO EventDecision (idMember, idEvent, decision, date)
	        VALUES ($member, $event, '$decision', $date);";
	sql_query($sql);
}

/*
 * @param event: id de l'évènement
 * @param member: id du membre
 * @param decision: Y ou N
 */
function event_update_decision($event, $member, $decision)
{	
	$event = intval($event);
	$member = intval($member);
	$decision = mysql_real_escape_string($decision);
	$date = time();
	$sql = "UPDATE EventDecision
	        SET decision = '$decision',
				date = $date
	        WHERE idMember = $member AND idEvent = $event;";
	sql_query($sql);
}


